package org.wu.framework.authorization.platform.infrastructure.persistence;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.wu.framework.authorization.platform.infrastructure.converter.SysMenuConverter;
import org.wu.framework.authorization.platform.infrastructure.converter.SysPermissionConverter;
import org.wu.framework.authorization.platform.infrastructure.converter.SysRoleConverter;
import org.wu.framework.authorization.platform.infrastructure.entity.*;
import org.wu.framework.authorization.platform.domain.model.sys.role.SysRole;
import org.wu.framework.authorization.platform.domain.model.sys.role.SysRoleRepository;
import org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.lazy.orm.web.plus.AbstractLazyCrudRepository;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * describe 角色
 *
 * @author Jia wei Wu
 * @date 2023/05/17 11:19 晚上
 * @see DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class SysRoleRepositoryImpl extends AbstractLazyCrudRepository<SysRoleDO, SysRole, Integer> implements SysRoleRepository {

    @Autowired
    LazyLambdaStream lazyLambdaStream;


    /**
     * describe 新增角色
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysRole> story(SysRole sysRole) {
        SysRoleDO sysRoleDO = SysRoleConverter.fromRole(sysRole);
        lazyLambdaStream.upsert(sysRoleDO);
        return ResultFactory.successOf(SysRoleConverter.toRole(sysRoleDO));
    }

    /**
     * describe 查询单个角色
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysRole> findOne(SysRole sysRole) {
        SysRoleDO sysRoleDO = SysRoleConverter.fromRole(sysRole);
        SysRole sysRoleOne = lazyLambdaStream.selectOne(LazyWrappers.lambdaWrapperBean(sysRoleDO), SysRole.class);
        // 获取角色对应的菜单
        List<SysMenuDO> sysMenuDOList = lazyLambdaStream.selectList(
                LazyWrappers.<SysRoleMenuDO>lambdaWrapper()
                        .eq(SysRoleMenuDO::getIsDeleted, false)
                        .eq(SysRoleMenuDO::getRoleId, sysRoleOne.getId())
                        .leftJoin(
                                LazyWrappers.<SysRoleMenuDO, SysMenuDO>lambdaWrapperJoin()
                                        .eq(SysRoleMenuDO::getMenuId, SysMenuDO::getId)
                                        .eqRighto(SysMenuDO::getIsDeleted,false)
                        )
                        .onlyUseAs()
                        .as(SysMenuDO.class),
                SysMenuDO.class
        );
        // 查询对应权限
        List<SysPermissionDO> sysPermissionDOList = lazyLambdaStream.selectList(
                LazyWrappers.<SysRolePermissionDO>lambdaWrapper()
                        .eq(SysRolePermissionDO::getIsDeleted, false)
                        .eq(SysRolePermissionDO::getRoleId, sysRoleOne.getId())
                        .leftJoin(
                                LazyWrappers.<SysRolePermissionDO, SysPermissionDO>lambdaWrapperJoin()
                                        .eq(SysRolePermissionDO::getPermissionId, SysPermissionDO::getId)
                                        .eqRighto(SysPermissionDO::getIsDeleted,false)
                        )
                        .onlyUseAs()
                        .as(SysPermissionDO.class),
                SysPermissionDO.class
        );
        sysRoleOne.setSysMenuList(sysMenuDOList.stream().map(SysMenuConverter::toMenu).collect(Collectors.toList()));
        sysRoleOne.setSysPermissionList(sysPermissionDOList.stream().map(SysPermissionConverter.INSTANCE::toSysPermission).collect(Collectors.toList()));
        return ResultFactory.successOf(sysRoleOne);
    }

    /**
     * describe 查询多个角色
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<List<SysRole>> findList(SysRole sysRole) {
        SysRoleDO sysRoleDO = SysRoleConverter.fromRole(sysRole);
        List<SysRole> sysRoleList = lazyLambdaStream.selectList(LazyWrappers.lambdaWrapperBean(sysRoleDO), SysRole.class);
        return ResultFactory.successOf(sysRoleList);
    }

    /**
     * describe 删除角色
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysRole> delete(SysRole sysRole) {
        SysRoleDO sysRoleDO = SysRoleConverter.fromRole(sysRole);
        //  lazyLambdaStream.remove(sysRoleDO);
        return ResultFactory.successOf();
    }

}